package cn.net.susan.service.sys;

import cn.hutool.core.bean.BeanUtil;
import cn.net.susan.dto.sys.MenuTreeDTO;
import cn.net.susan.dto.sys.MetaDTO;
import cn.net.susan.entity.ResponsePageEntity;
import cn.net.susan.entity.sys.MenuConditionEntity;
import cn.net.susan.entity.sys.MenuEntity;
import cn.net.susan.mapper.sys.MenuMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 菜单 服务层
 * 
 * @author 苏三 该项目是知识星球：java突击队 的内部项目
 * @date 2024-01-08 14:03:44
 */
@Service
public class MenuService {

	@Autowired
	private MenuMapper menuMapper;

	public List<MenuTreeDTO> getMenuTree() {
		MenuConditionEntity menuConditionEntity = new MenuConditionEntity();
		// 不设置pageSize=0，避免LIMIT 0, 0返回空数据
		// 对于菜单树查询，我们希望获取所有顶级菜单
		menuConditionEntity.setPid(0L);
		List<MenuEntity> menuEntities = menuMapper.searchByCondition(menuConditionEntity);
		if (CollectionUtils.isEmpty(menuEntities)) {
			return Collections.emptyList();
		}

		List<MenuTreeDTO> result = Lists.newArrayList();
		for (MenuEntity menuEntity : menuEntities) {
			MenuTreeDTO menuTreeDTO = buildMenuTreeDTO(menuEntity);
			menuTreeDTO.setAlwaysShow(true);
			result.add(menuTreeDTO);
			buildChildren(menuEntity, menuTreeDTO);
		}
		return result;
	}

	private void buildChildren(MenuEntity menuEntity, MenuTreeDTO menuTreeDTO) {
		MenuConditionEntity menuConditionEntity = new MenuConditionEntity();
		// 不设置pageSize=0，避免LIMIT 0, 0返回空数据
		menuConditionEntity.setPid(menuEntity.getId());
		List<MenuEntity> childrenEntities = menuMapper.searchByCondition(menuConditionEntity);
		if (CollectionUtils.isNotEmpty(childrenEntities)) {
			for (MenuEntity childrenEntity : childrenEntities) {
				MenuTreeDTO childrenMenuTreeDTO = buildMenuTreeDTO(childrenEntity);
				menuTreeDTO.addChildren(childrenMenuTreeDTO);
				buildChildren(childrenEntity, childrenMenuTreeDTO);
			}
		}
	}


	private MenuTreeDTO buildMenuTreeDTO(MenuEntity menuEntity) {
		MenuTreeDTO menuTreeDTO = BeanUtil.copyProperties(menuEntity, MenuTreeDTO.class);
		menuTreeDTO.setAlwaysShow(false);
		MetaDTO metaDTO = new MetaDTO();
		menuTreeDTO.setMeta(metaDTO);
		metaDTO.setIcon(menuTreeDTO.getIcon());
		metaDTO.setTitle(menuTreeDTO.getName());
		metaDTO.setNoCache(true);
		return menuTreeDTO;
	}

	/**
     * 查询菜单信息
     * 
     * @param id 菜单ID
     * @return 菜单信息
     */
	public MenuEntity findById(Long id) {
	    return menuMapper.findById(id);
	}
	
	/**
     * 根据条件分页查询菜单列表
     * 
     * @param menuConditionEntity 菜单信息
     * @return 菜单集合
     */
	public ResponsePageEntity<MenuEntity> searchByPage(MenuConditionEntity menuConditionEntity) {
		int count = menuMapper.searchCount(menuConditionEntity);
		if (count == 0) {
			return ResponsePageEntity.buildEmpty(menuConditionEntity);
		}
		List<MenuEntity> dataList = menuMapper.searchByCondition(menuConditionEntity);
		return ResponsePageEntity.build(menuConditionEntity, count, dataList);
	}
	
    /**
     * 新增菜单
     * 
     * @param menuEntity 菜单信息
     * @return 结果
     */
	public int insert(MenuEntity menuEntity) {
	    return menuMapper.insert(menuEntity);
	}
	
	/**
     * 修改菜单
     * 
     * @param menuEntity 菜单信息
     * @return 结果
     */
	public int update(MenuEntity menuEntity) {
	    return menuMapper.update(menuEntity);
	}

	/**
     * 删除菜单对象
     * 
     * @param id 系统ID
     * @return 结果
     */
	public int deleteById(Long id) {
		return menuMapper.deleteById(id);
	}
	
}
